package com.hk.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hk.model.News;
import com.hk.model.Product;
import com.hk.model.ProductMenu;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.IProductMenuService;
import com.hk.service.IProductService;
import com.hk.service.impl.ProductMenuServiceImpl;
import com.hk.service.impl.ProductServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/19 12:30
 * @Statement: 查询所有产品和新增
 */
@WebServlet("/product/ProductQueryOrSave")
public class ProductQueryOrSaveServlet extends HttpServlet {

    IProductService iProductService = new ProductServiceImpl();

    IProductMenuService iProductMenuService = new ProductMenuServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String page = Tool.nullToString(req.getParameter("page"));
        String limit = Tool.nullToString(req.getParameter("limit"));
        String name = Tool.nullToString(req.getParameter("name"));
        String platformPrice = Tool.nullToString(req.getParameter("platformPrice"));
        String marketPrice = Tool.nullToString(req.getParameter("marketPrice"));
        String tMenuId = Tool.nullToString(req.getParameter("tMenuId"));
        int i = Tool.parseInt(page);
        int size = Tool.parseInt(limit);
//        封装数据
        Product product = product = new Product();
        BigDecimal platformPrice2 = Tool.parseBigDecimal(platformPrice);
        BigDecimal marketPrice2 = Tool.parseBigDecimal(marketPrice);
        int inttMenuId = Tool.parseInt(tMenuId);
        product.setName(name);
        product.setPlatformPrice(platformPrice2);
        product.setMarketPrice(marketPrice2);
        product.settMenuId(inttMenuId);
//        System.out.println("product="+product);
//        根据sql语句规则将每页页码修改为需要数值
        int pageNum = Tool.pageNum(i,size);
        List<Product> productList = iProductService.findAll(pageNum,size,product);
        int count = iProductService.count(-1,-1, product);
        PrintWriter out = resp.getWriter();
        if (productList.isEmpty()) {
            ResultVO<Product> resultVO = new ResultVO<>(0,"error",0,productList);
            String productListJSON = JSONObject.toJSONString(resultVO);
            out.println(productListJSON);
        } else {
            ResultVO<Product> resultVO = new ResultVO<>(0,"success",count,productList);
            String productListJSON = JSONObject.toJSONString(resultVO);
            out.println(productListJSON);
        }
        out.flush();
        out.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        Product product = gson.fromJson(reader, Product.class);
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        if (product.gettMenuId() < 1) {
            result.setStatus(0);
            result.setMessage("error");
            String resultJSON = JSONObject.toJSONString(result);
            out.println(resultJSON);
            out.flush();
            out.close();
        }
        //        通过子类型Id查询父类型Id并进行赋值
        ProductMenu productMenu = iProductMenuService.findById(product.gettMenuId());
        product.setMenuId(productMenu.getParentId());
        int save = iProductService.save(product);
        if (save < 1) {
            result.setStatus(save);
            result.setMessage("error");
        } else {
            result.setStatus(save);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();

    }
}
